﻿using UnityEngine;
using System.Collections;

public class NetworkConnection : MonoBehaviour 
{
    /* Check this: http://www.palladiumgames.net/tutorials/unity-networking-tutorial/
     * 
     * I am going to use a peer-to-peer method
    */
    public string connectionIP = "10.51.50.43";
    public int connectionPort = 25001;
    public int nbConnectionMax = 32;

    public GameObject _prefabPlayer1;
    public GameObject _prefabPlayer2;

    public Vector3 _positionPlayer1;
    public Vector3 _positionPlayer2;

    private bool _player1HasSpawned = false;
    private bool _player2HasSpawned = false;
    private HostData[] _listServ;

	// Use this for initialization
	void Start () 
    {
        DontDestroyOnLoad(this.gameObject); //Keep the connection status between level
	}

    void OnGUI()
    {
        if (Network.peerType == NetworkPeerType.Disconnected)
        {
            GUI.Label(new Rect(10, 10, 300, 20), "Status: Disconnected");
            if (GUI.Button(new Rect(10, 30, 120, 20), "Client Connect"))
            {
                MasterServer.RequestHostList("Warpath");
                _listServ = MasterServer.PollHostList();
                if (_listServ[0] != null)
                {
                    Network.Connect(_listServ[0]);
                }
                
            }
            if (GUI.Button(new Rect(10, 50, 120, 20), "Initialize Server"))
            {
                Network.InitializeServer(nbConnectionMax, connectionPort, false);
                MasterServer.RegisterHost("Warpath", "God game", "gg no re");
            }
        }
        else if (Network.peerType == NetworkPeerType.Client)
        {
            GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as Client");

            if (!_player2HasSpawned)
            {
                _player2HasSpawned = true;//Useless, is not send on the network

                networkView.RPC("LoadLevel", RPCMode.All, 1); // If server is connected, then then game can start. Maybe not the best check (limited to 2 players)

                //Network.Instantiate(_prefabPlayer2, _positionPlayer2, Quaternion.identity, 0); //Maybe check for communication group (last parameter). Not sure that's needed.
            }

            if (GUI.Button(new Rect(10, 30, 120, 20), "Disconnect"))
            {
                networkView.RPC("LoadLevel", RPCMode.All, 0); // Seem to work
                Network.Disconnect(200);
            }
        }
        else if (Network.peerType == NetworkPeerType.Server)
        {
            GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as Server");

            if (!_player1HasSpawned)
            {
                _player1HasSpawned = true; //Useless, is not send on the network
                //Network.Instantiate(_prefabPlayer1, _positionPlayer1, Quaternion.identity, 0); //Maybe check for communication group (last parameter). Not sure that's needed.
            }

            if (GUI.Button(new Rect(10, 30, 120, 20), "Disconnect"))
            {
                Network.Disconnect(200);
            }
        }
    }


    void OnPlayerDisconnected(NetworkPlayer player) //Maybe not needed //Delete only on serveur, not on deconnected pc
    {
        Debug.Log("Clean up after player " + player);
        Network.RemoveRPCs(player);
        networkView.RPC("LoadLevel", RPCMode.All, 0); // Seem to work
        
    }

	// Update is called once per frame
	void Update () 
    {

	}

    [RPC]
    public void LoadLevel(int level)
    {
        Application.LoadLevel(level);
    }
}
